﻿// <copyright file="XamlGenerator.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.Xml
{
    using System.Windows;
    using System.Windows.Media;
    using System.Xml.Linq;

    /// <summary>
    /// Xaml Generator class
    /// </summary>
    internal static partial class XamlGenerator
    {
        /// <summary>
        /// Keys the allocated label styles.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Allocated Label styles XElement</returns>
        internal static XElement GenerateKeyAllocatedLabelStyles(XNamespace ns)
        {
            XElement element = new XElement(
                ns + XamlType.Style,
                new XAttribute("{http://schemas.microsoft.com/winfx/2006/xaml}Key", "KeyAllocToolBarLabel"),
                new XAttribute("TargetType", "{x:Type Label}"),
                new XElement(ns + "Setter", new XAttribute("Property", "FontWeight"), new XAttribute("Value", "SemiBold")));
            return element;
        }

        /// <summary>
        /// Generates the translate transform.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="x">The x axis.</param>
        /// <param name="y">The y axis.</param>
        /// <returns>The translate transform XElement</returns>
        internal static XElement GenerateTranslateTransform(XNamespace ns, decimal x, decimal y)
        {
            return new XElement(
                ns + XamlType.TranslateTransform,
                new XAttribute("X", x),
                new XAttribute("Y", y));
        }

        /// <summary>
        /// Generates the rotate transform.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="angle">The angle.</param>
        /// <returns>The Rotate Transform XElement</returns>
        internal static XElement GenerateRotateTransform(XNamespace ns, decimal angle)
        {
            return new XElement(
                ns + XamlType.RotateTransform,
                new XAttribute("Angle", angle));
        }

        /// <summary>
        /// Generates the skew transform.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="angleX">The angle X.</param>
        /// <param name="angleY">The angle Y.</param>
        /// <returns>Skew Transform XElement</returns>
        internal static XElement GenerateSkewTransform(XNamespace ns, decimal angleX, decimal angleY)
        {
            return new XElement(
                ns + XamlType.SkewTransform,
                new XAttribute("AngleX", angleX),
                new XAttribute("AngleY", angleY));
        }

        /// <summary>
        /// Generates the scale transform.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="scaleX">The scale X.</param>
        /// <param name="scaleY">The scale Y.</param>
        /// <returns>The scale transform XElement</returns>
        internal static XElement GenerateScaleTransform(XNamespace ns, decimal scaleX, decimal scaleY)
        {
            return new XElement(
                ns + XamlType.ScaleTransform,
                new XAttribute("ScaleX", scaleX),
                new XAttribute("ScaleY", scaleY));
        }

        /// <summary>
        /// Panels the background style.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Panel Background Style</returns>
        internal static XElement GeneratePanelBackgroundStyle(XNamespace ns)
        {
            XElement element = new XElement(
                ns + XamlType.Style,
                new XAttribute("{http://schemas.microsoft.com/winfx/2006/xaml}Key", "PanelBackground"),
                new XAttribute("TargetType", "{x:Type Panel}"),
                   new XElement(ns + "Setter", new XAttribute("Property", "Background"), new XElement(ns + "Setter.Value", GenerateLinearGradientBrush(ns, "0,0.5", "1,0.5"))));
            return element;
        }

        /// <summary>
        /// Generates the linear gradient brush.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="startPoint">The start point.</param>
        /// <param name="endPoint">The end point.</param>
        /// <returns>Linear Gradient Brush</returns>
        internal static XElement GenerateLinearGradientBrush(XNamespace ns, string startPoint, string endPoint)
        {
            return new XElement(
                ns + XamlType.LinearGradientBrush,
                new XAttribute("StartPoint", startPoint),
                new XAttribute("EndPoint", endPoint),
                GenerateLinearGradientBrushRelativeTransform(ns),
                    GenerateLinearGradientBrushGradientStops(ns));
        }

        /// <summary>
        /// Generates the linear gradient brush relative transform.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Linear Gradient Brush Relative Transform</returns>
        internal static XElement GenerateLinearGradientBrushRelativeTransform(XNamespace ns)
        {
            return new XElement(
                ns + XamlType.LinearGradientBrushRelativeTransform,
                GenerateTransformGroup(ns));
        }

        /// <summary>
        /// Generates the linear gradient brush gradient stops.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Linear Gradient Brush Gradient Stops</returns>
        internal static XElement GenerateLinearGradientBrushGradientStops(XNamespace ns)
        {
            return new XElement(
                ns + XamlType.LinearGradientBrushGradientStops,
                GenerateGradientStop(ns, Colors.Silver, 0.0m),
                GenerateGradientStop(ns, Colors.White, 1.5m));
        }

        /// <summary>
        /// Generates the gradient stop.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="color">The color.</param>
        /// <param name="offset">The offset.</param>
        /// <returns>Graident stop</returns>
        internal static XElement GenerateGradientStop(XNamespace ns, Color color, decimal offset)
        {
            return new XElement(
                ns + XamlType.GradientStop,
                new XAttribute("Color", color),
                new XAttribute("Offset", offset));
        }

        /// <summary>
        /// Generates the transform group.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Transform group</returns>
        internal static XElement GenerateTransformGroup(XNamespace ns)
        {
            return new XElement(
                ns + XamlType.TransformGroup,
                GenerateTranslateTransform(ns, -0.5m, -0.5m),
                GenerateScaleTransform(ns, 1, 1),
                GenerateSkewTransform(ns, 0m, 0m),
                GenerateRotateTransform(ns, 90.0172795589426m),
                GenerateTranslateTransform(ns, -0.5m, -0.5m),
                GenerateTranslateTransform(ns, -0.0015136743804197244m, -0.19736842105263153m));
        }

        /// <summary>
        /// Generates the name text block.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="name">The name of the product.</param>
        /// <returns>Name Text Block</returns>
        internal static XElement GenerateNameTextBlock(XNamespace ns, string name)
        {
            return new XElement(
                ns + XamlType.TextBlock,
               GenerateFontWeightAttribute(FontWeights.SemiBold),
               GenerateMarginAttribute("4"),
               new XAttribute("Text", name));
        }

        /// <summary>
        /// Generates the notes label text block.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <returns>Notes Label Text Block</returns>
        internal static XElement GenerateNotesLabelTextBlock(XNamespace ns)
        {
            return new XElement(
                ns + "TextBlock",
               GenerateFontWeightAttribute(FontWeights.SemiBold),
               GenerateMarginAttribute("4"),
               new XAttribute("Text", "Notes: "));
        }

        /// <summary>
        /// Generates the notes text block.
        /// </summary>
        /// <param name="ns">The namespace.</param>
        /// <param name="notes">The notes.</param>
        /// <returns>Notes text block</returns>
        internal static XElement GenerateNotesTextBlock(XNamespace ns, string notes)
        {
            return new XElement(
                ns + "TextBlock",
               GenerateMarginAttribute("4"),
               new XAttribute("Text", string.IsNullOrEmpty(notes) ? string.Empty : notes));
        }
    }
}
